package com.xywl.escort.view.setting;

import com.xywl.escort.bean.BaseReq;
import com.xywl.escort.bean.BaseResp;
import com.xywl.escort.bean.FindIsFitPayPwdResp;
import com.xywl.escort.bean.UpgradeReq;
import com.xywl.escort.bean.UpgradeResp;
import com.xywl.escort.bean.UserExitReq;
import com.xywl.escort.net.BaseSubscriber;
import com.xywl.escort.net.ExceptionHandle;
import com.xywl.escort.net.api.service.ApiServer;
import com.xywl.escort.utils.RxUtil;

import javax.inject.Inject;

/**
 * Created by 32422 on 2017/10/25.
 */

public class SettingsPresenter implements SettingsContract.Presenter {
    private SettingsContract.View mView;
    private ApiServer mApiServer;

    @Inject
    SettingsPresenter(ApiServer apiServer) {
        mApiServer = apiServer;
    }

    @Override
    public void subscribe() {

    }

    @Override
    public void unSubscribe() {
        mView = null;
    }

    @Override
    public void takeView(SettingsContract.View view) {
        this.mView = view;
    }

    @Override
    public void isSetPayPwd(String userId) {
        BaseReq req = new BaseReq();
        req.userId = userId;
        mApiServer.isSetPayPwd(req)
                .compose(RxUtil.<FindIsFitPayPwdResp>applySchedulers(RxUtil.IO_ON_UI_TRANSFORMER_BACK_PRESSURE))
                .compose(mView.<FindIsFitPayPwdResp>bindToLife())
                .subscribe(new BaseSubscriber<FindIsFitPayPwdResp>() {
                    @Override
                    protected void hideDialog() {

                    }

                    @Override
                    protected void showDialog() {

                    }

                    @Override
                    public void onError(ExceptionHandle.ResponseThrowable e) {
                        mView.findIsFitPayPwdFailure("验证支付密码失败");
                    }

                    @Override
                    public void onNext(FindIsFitPayPwdResp resp) {
                        if (null != resp) {
                            if (resp.isOk()) {
                                if (null != resp.data) {
                                    if (resp.data.status == 1) {
                                        mView.hasSetPayPwd();
                                    } else if (resp.data.status == 0){
                                        mView.noSetPayPwd();
                                    } else {
                                        mView.findIsFitPayPwdFailure(resp.msg);
                                    }
                                } else {
                                    mView.findIsFitPayPwdFailure(resp.msg);
                                }
                            } else {
                                mView.findIsFitPayPwdFailure(resp.msg);
                            }
                        } else {
                            mView.findIsFitPayPwdFailure("验证支付密码失败");
                        }
                    }
                });
    }

    @Override
    public void upgrade(int updateObject, final int versionCode, final int type) {
        UpgradeReq req = new UpgradeReq();
        req.updateObject = updateObject;
        mApiServer.upgrade(req)
                .compose(RxUtil.<UpgradeResp>applySchedulers(RxUtil.IO_ON_UI_TRANSFORMER_BACK_PRESSURE))
                .compose(mView.<UpgradeResp>bindToLife())
                .subscribe(new BaseSubscriber<UpgradeResp>() {
                    @Override
                    protected void hideDialog() {

                    }

                    @Override
                    protected void showDialog() {

                    }

                    @Override
                    public void onError(ExceptionHandle.ResponseThrowable e) {
                        mView.noUpgrade("已是最新版", type);
                    }

                    @Override
                    public void onNext(UpgradeResp resp) {
                        if (null != resp && null != resp.data) {
                            if (resp.isOk()) {
                                int inserVersionNumber = resp.data.innserVersionNumber;
                                if (inserVersionNumber > versionCode) {
                                    mView.hasUpgrade(resp.data);
                                } else {
                                    mView.noUpgrade("已是最新版", type);
                                }
                            } else {
                                mView.noUpgrade("已是最新版", type);
                            }
                        } else {
                            mView.noUpgrade("已是最新版", type);
                        }
                    }
                });
    }

    @Override
    public void userExit(String token, String deviceId) {
        UserExitReq req = new UserExitReq();
        req.token = token;
        req.deviceId = deviceId;
        mApiServer.userExit(req)
                .compose(RxUtil.<BaseResp>applySchedulers(RxUtil.IO_ON_UI_TRANSFORMER_BACK_PRESSURE))
                .compose(mView.<BaseResp>bindToLife())
                .subscribe(new BaseSubscriber<BaseResp>() {
                    @Override
                    protected void hideDialog() {

                    }

                    @Override
                    protected void showDialog() {

                    }

                    @Override
                    public void onError(ExceptionHandle.ResponseThrowable e) {
                        mView.userExitFailure();
                    }

                    @Override
                    public void onNext(BaseResp resp) {
                        if (null != resp && resp.isOk()) {
                            mView.userExitSuccess(resp);
                        } else {
                            mView.userExitFailure();
                        }
                    }
                });
    }
}
